package il.ac.technion.wiki.api;

import static org.junit.Assert.fail;

import java.util.List;

import junit.framework.Assert;
import il.ac.technion.wiki.api.wikidocument.IWikiDocument;
import il.ac.technion.wiki.api.wikidocument.WikiDocument;

import org.junit.After;
import org.junit.Test;

public class WikipediaTest {
	IWikiDocument doc1 = new WikiDocument("1", "hello world");
	IWikiDocument doc2 = new WikiDocument("1", "good morning world");
	IWikiDocument doc3 = new WikiDocument("2", "hi there world");
	Wikipedia wiki = Wikipedia.INSTANCE;
	
	@After
	public void teadDown(){
		wiki.removeDocument("1");
		wiki.removeDocument("2");
	}
	
	@Test
	public void testBasicOperations() {
		// Test add
		wiki.addDocument(doc1);
		try{
			wiki.addDocument(doc2);
			fail("inserted another document with the same title");
		} catch(IllegalArgumentException e){}

		// Test Update
		wiki.updateDocument(doc2);
		IWikiDocument myDoc1 = wiki.getDocumentByTitle("1");
		Assert.assertEquals("good morning world", myDoc1.getContent());
		
		// Test remove
		wiki.addDocument(doc3);
		wiki.removeDocument("2");
		if (wiki.getDocumentByTitle("2") != null)
			fail("didn't remove the object");
	}

	@Test
	public void testSearch() {
		wiki.addDocument(doc1);
		wiki.addDocument(doc3);
		
		List<IWikiDocument> hiSearch = wiki.Search("\"hi\"");
		if (hiSearch.size() != 1)
			fail("find more or less that the single document answering the query");
		if (hiSearch.get(0).getTitle() != "2")
			fail("didn't return the right document");

		List<IWikiDocument> hiOrWorldSearch = wiki.Search("\"hi\" or \"world\"");
		if (hiOrWorldSearch.size() != 2)
			fail("find more or less that the single document answering the query");
		if ((hiOrWorldSearch.get(0).getTitle() != "2") && (hiOrWorldSearch.get(1).getTitle() != "1"))
			fail("didn't return the documents on the right orded");
	}
}
